package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author fy
 * @version V1.0
 * @Project day41
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/20 14:34
 */
@WebServlet("/admin")
public class AdminController extends BaseServlet{

    public String login(HttpServletRequest request, HttpServletResponse response){

        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2：判断用户名密码不为空，如果为空跳转到登陆页面
        if(username==null||password==null){
            return "redirect:" + request.getContextPath()+"/admin/login.jsp";
        }
        //3：调用service校验用户名密码
        AdminService adminService = new AdminServiceImpl();
        User user = adminService.login(username, password);

        //4：根据结果重定向操作
        if(user !=null){
            //5：将登陆成功后的对象放入tomcat的session会话中一直携带
            request.getSession().setAttribute("admin", user);

            //跳转到管理员首页
            return "redirect:"+request.getContextPath()+"/admin/admin.jsp";

        }else {
            return "redirect:"+request.getContextPath()+"/admin/login.jsp";
        }
        //System.out.println("========登录成功==========");
//        request.setAttribute("admin", "管理员");
        //跳转到管理员首页
        //return "redirect:"+request.getContextPath()+"/admin/admin.jsp";
    }

}
